package com.itheima.controller.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.itheima.domain.Role;
import com.itheima.service.RoleService;
import com.itheima.service.impl.RoleServiceImpl;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/roleServlet")
public class RoleServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=utf-8");
        String method = request.getParameter("method");
        Map resultMap = null;
        ConvertUtils.register(new Converter() {
            @Override
            public Object convert(Class aClass, Object o) {
                if (o == null){
                    return null;
                }
                String str = (String) o;
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                Date date = null;
                try {
                    date = sdf.parse(str);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                return date;
            }
        }, Date.class);
        if ("findAll".equals(method)){
            resultMap = this.findAll(request,response);
        } else if ("add".equals(method)){
            resultMap = this.add(request,response);
        }else if ("update".equals(method)){
            resultMap = this.update(request,response);
        }else if ("delete".equals(method)){
            resultMap = this.delete(request,response);
        }

        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(resultMap);
        response.getWriter().write(json);

    }

    private Map delete(HttpServletRequest request, HttpServletResponse response) {
        Map resultMap = new HashMap();
        try {
            String id = request.getParameter("id");
            service.delete(id);
            resultMap.put("flag",true);
        } catch (Exception e) {
            resultMap.put("flag",false);
            resultMap.put("message","删除失败!");
            e.printStackTrace();
        }
        return resultMap;
    }

    private Map update(HttpServletRequest request, HttpServletResponse response) {
        Map resultMap = new HashMap();
        try {
            Map<String, String[]> map = request.getParameterMap();
            Role role = new Role();
            BeanUtils.populate(role,map);
            service.update(role);
            resultMap.put("flag",true);
        } catch (Exception e) {
            resultMap.put("flag",false);
            resultMap.put("message","修改失败!");
            e.printStackTrace();
        }
        return resultMap;
    }

    private Map add(HttpServletRequest request, HttpServletResponse response) {
        Map resultMap = new HashMap();
        try {
            Map<String, String[]> map = request.getParameterMap();
            Role role = new Role();
            BeanUtils.populate(role,map);
            service.add(role);
            resultMap.put("flag",true);
        } catch (Exception e) {
            resultMap.put("flag",false);
            resultMap.put("message","添加失败!");
            e.printStackTrace();
        }
        return resultMap;
    }

    private RoleService service = new RoleServiceImpl();
    private Map findAll(HttpServletRequest request, HttpServletResponse response) {
        Map resultMap = new HashMap();
        try {
            List<Role> list = service.findAll();
            resultMap.put("flag",true);
            resultMap.put("resultData",list);
        } catch (Exception e) {
            resultMap.put("flag",false);
            resultMap.put("message","查询失败!");
            e.printStackTrace();
        }

        return resultMap;
    }
}
